package com.group01.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.group01.dto.Address.AddressInsertInfoDTO;
import com.group01.dto.Address.AddressUpdateInfoDTO;
import com.group01.enums.CodeAndMessageEnum;
import com.group01.mapper.UserMapper;
import com.group01.pojo.Address;
import com.group01.pojo.User;
import com.group01.result.Result;
import com.group01.service.AddressService;
import com.group01.mapper.AddressMapper;
import com.group01.utils.BeanCopyUtils;
import com.group01.utils.CommonUtils;
import com.group01.vo.Address.AddressGetInfoVO;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 19559
* @description 针对表【es_address(收货地址表)】的数据库操作Service实现
* @createDate 2024-06-23 10:14:14
*/
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements AddressService{

    /**
     * 注入
     */
    private final AddressMapper addressMapper;
    private final UserMapper userMapper;

    public AddressServiceImpl(AddressMapper addressMapper, UserMapper userMapper) {
        this.addressMapper = addressMapper;
        this.userMapper = userMapper;
    }

    /**
     * 获取用户地址列表
     * @return {@link Result }<{@link List }<{@link AddressGetInfoVO }>>
     */
    @Override
    public Result<List<AddressGetInfoVO>> getAddressList() {
        /*
         * 获取用户id
         */
        Integer userId = CommonUtils.getLocalId();
        QueryWrapper<AddressGetInfoVO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("a.user_id" , userId);

        List<AddressGetInfoVO> addressList = addressMapper.getAllAddress(queryWrapper);
        return Result.success(CodeAndMessageEnum.FIND_RESULT, addressList);
    }

    /**
     * 新增收货地址
     * @param addressInsertInfoDTO 收货地址传输对象DTO
     * @return {@link Result }<{@link String }>
     */
    @Override
    public Result<String> addAddress(AddressInsertInfoDTO addressInsertInfoDTO) {
        Long count = userMapper.selectCount(new LambdaQueryWrapper<User>().eq(User::getId, addressInsertInfoDTO.getUserId()));
        if (count == 0) {
            return Result.error(CodeAndMessageEnum.USER_NOT_EXIST);
        }
        LambdaQueryWrapper<Address> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(Address::getDetailAddr, addressInsertInfoDTO.getDetailAddr())
                .eq(Address::getPhone, addressInsertInfoDTO.getPhone())
                .eq(Address::getReceiverName, addressInsertInfoDTO.getReceiverName())
                .eq(Address::getUrbanAddr, addressInsertInfoDTO.getUrbanAddr())
                .eq(Address::getUserId, addressInsertInfoDTO.getUserId());
        Address address = addressMapper.selectOne(queryWrapper);
        if (address != null) {
            return Result.error(CodeAndMessageEnum.ADDRESS_EXIST);
        }

        addressMapper.insert(BeanCopyUtils.copyBean(addressInsertInfoDTO, Address.class));

        return Result.success(CodeAndMessageEnum.SUCCESS_ADD_DELIVERY);
    }

    /**
     * 修改地址信息
     * @param addressUpdateInfoDTO 地址传输对象DTO
     * @return {@link Result }<{@link String }>
     */
    @Override
    public Result<String> updateAddress(AddressUpdateInfoDTO addressUpdateInfoDTO) {
        if(addressMapper.selectCount(new LambdaQueryWrapper<Address>().eq(Address::getId, addressUpdateInfoDTO.getId())) == 0) {
            return Result.error(CodeAndMessageEnum.ADDRESS_NOT_EXIST);
        }

        addressMapper.updateById(BeanCopyUtils.copyBean(addressUpdateInfoDTO, Address.class));
        return Result.success(CodeAndMessageEnum.SUCCESS_UPDATE_DELIVERY);
    }

    /**
     * 设置用户默认收货地址
     * @param addressId 地址id
     * @return {@link Result }<{@link String }>
     */
    @Override
    public Result<String> setDefault(Integer addressId) {

        Address address = addressMapper.selectById(addressId);
        if(address == null) {
            return Result.error(CodeAndMessageEnum.ADDRESS_NOT_EXIST);
        }

        /*
         * 获取用户id
         */
        Integer userId = CommonUtils.getLocalId();
        User user = userMapper.selectById(userId);

        if(addressMapper.checkUserAddress(userId , addressId) == 0) {
            return Result.error("该地址不存在,请先添加新地址!");
        }
        /*
         * 更新用户表中该用户的默认地址
         */
        LambdaUpdateWrapper<User> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper
                .eq(User::getId, userId)
                .set(User::getDefaultAddressId, addressId);

        userMapper.update(user, queryWrapper);

        return Result.success(CodeAndMessageEnum.SUCCESS_SET_DEFAULT_DELIVERY);
    }

    /**
     * 取消用户默认收货地址
     * @param addressId 地址id
     * @return {@link Result }<{@link String }>
     */
    @Override
    public Result<String> cancelDefault(Integer addressId) {
        /*
         * 获取用户id
         */
        Integer userId = CommonUtils.getLocalId();

        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getId , userId)
                .eq(User::getDefaultAddressId, addressId);

        if(addressMapper.checkUserAddress(userId , addressId) == 0
            || userMapper.selectCount(queryWrapper) == 0) {
            return Result.error("该地址不存在,请先添加新地址!");
        }

        LambdaUpdateWrapper<User> queryWrapper1 = new LambdaUpdateWrapper<>();
        queryWrapper1.eq(User::getId , userId)
                .set(User::getDefaultAddressId , null);

        userMapper.update(queryWrapper1);

        return Result.success(CodeAndMessageEnum.SUCCESS_CANCEL_DEFAULT_DELIVERY);
    }

    /**
     * 删除收货地址
     * @param id 地址id
     * @return {@link Result }<{@link String }>
     */
    @Override
    public Result<String> deleteAddress(Integer id) {
        /*
         * 获取用户id
         */
        Integer userId = 2;
        if(addressMapper.checkUserAddress(userId , id) == 0) {
            return Result.error(CodeAndMessageEnum.ADDRESS_NOT_EXIST);
        }
        addressMapper.deleteById(id);
        return Result.success(CodeAndMessageEnum.SUCCESS_DELETE_DELIVERY);
    }
}




